﻿using System;
using System.Runtime.InteropServices;

namespace BGShell.PowerShell.Interop
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal sealed class SHFILEINFO
    {
        private SHFILEINFO()
        {
        }

        public readonly IntPtr hIcon;
        public readonly Int32 iIcon;
        public readonly Int32 dwAttributes;
        
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public readonly String szDisplayName;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public readonly String szTypeName;

        public static SHFILEINFO GetFileInfo(String path, Int32 attributes, SHGetFileInfoFlags flags)
        {
            var info = new SHFILEINFO();
            
            if (NativeMethods.SHGetFileInfoW(path, attributes, info, Marshal.SizeOf(info), flags) == IntPtr.Zero)
            {
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            return info;
        }
    }
}
